package cn.kys.modules.system.controller;

import cn.kys.modules.system.service.MobileService;
import cn.kys.modules.system.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/phone_api")
public class PhoneApi {

    @Autowired
    private PhoneService phoneService;

    @GetMapping("/getTasks")
    public Map getTasks(){
        Map returnMap = new HashMap();
        try {
            List list = phoneService.getTasks();
            returnMap.put("data",list);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //身份证号、方案号，查询自己家的地块
    @GetMapping("/getDK")
    public Map getDK(@RequestParam("taskid") String taskid,@RequestParam("idCard") String idCard,@RequestParam("caseid") String caseid){
        Map returnMap = new HashMap();
        try {
            Map map = phoneService.getDK(taskid,idCard,caseid);
            returnMap.put("data",map);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }
    //身份证号、任务编号、地块编号
    @GetMapping("/submitTableContent")
    public Map submitTableContent(@RequestParam("idCard") String idCard,@RequestParam("taskId") String taskId,@RequestParam("gid") String gid,@RequestParam("content") String content){
        Map returnMap = new HashMap();
        try {
            phoneService.submitTableContent(idCard,taskId,gid,content);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //更新状态
    @GetMapping("/updateStatus")
    public Map updateStatus(@RequestParam("uuid") String uuid,@RequestParam("status") String status){
        Map returnMap = new HashMap();
        try {
            phoneService.updateStatus(uuid,status);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //提交身份证正反面
    @PostMapping("/submitCardImg")
    public Map submitCardImg(@RequestParam("idCard") String idCard,@RequestParam("uploadFile") MultipartFile uploadFile
            ,@RequestParam("type") String type){
        Map returnMap = new HashMap();
        try {
            Map map = phoneService.submitCardImg(idCard,uploadFile,type);
            returnMap.put("data",map);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //提交表单,id为子任务的id,uploadFile为图片
    @PostMapping("/submitTableFile")
    public Map submitTableFile(@RequestParam("idCard") String idCard,@RequestParam("taskid") String taskid,@RequestParam("gid") String gid,@RequestParam("formcontentname") String formcontentname,@RequestParam("uploadFile") MultipartFile uploadFile
            ,@RequestParam("alpha") String alpha
            ,@RequestParam("beta") String beta
            ,@RequestParam("gamma") String gamma
            ,@RequestParam("lat") String lat
            ,@RequestParam("lon") String lon){
        Map returnMap = new HashMap();
        try {
            String filename = phoneService.submitTableFile(idCard,taskid,gid,formcontentname,uploadFile,alpha,beta,gamma,lat,lon);
            returnMap.put("data",filename);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //查询表单内容
    @GetMapping("/queryTables")
    public Map queryTables(@RequestParam("caseid") String caseid,@RequestParam("idCard") String idCard,@RequestParam("taskid") String taskid,@RequestParam("gid") String gid){
        Map returnMap = new HashMap();
        try {
            Map map = phoneService.queryTables(caseid,idCard,taskid,gid);
            returnMap.put("data",map);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //查询表单内容，状态不是未开始
    @GetMapping("/queryTablesPC")
    public Map queryTablesPC(@RequestParam("taskid") String taskid,@RequestParam("caseid") String caseid,@RequestParam("status") String status,
        @RequestParam("curPage")int curPage, @RequestParam("pageSize")int pageSize){
        Map returnMap = new HashMap();
        try {
            Map map = phoneService.queryTablesPC(taskid,caseid,status,curPage,pageSize);
            returnMap.put("data",map);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //查询表单内容，状态不是未开始
    @GetMapping("/queryTablesPCByCard")
    public Map queryTablesPCByCard(@RequestParam("taskid") String taskid,@RequestParam("caseid") String caseid,@RequestParam("idCard") String idCard
        ,@RequestParam("curPage")int curPage, @RequestParam("pageSize")int pageSize){
        Map returnMap = new HashMap();
        try {
            Map map = phoneService.queryTablesPCByCard(taskid,caseid,idCard,curPage,pageSize);
            returnMap.put("data",map);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }


    //查询图斑和照片
    @GetMapping("/queryGeoAndImagePC")
    public Map queryGeoAndImagePC(@RequestParam("idCard") String idCard,@RequestParam("taskid") String taskid,@RequestParam("caseid") String caseid,@RequestParam("gid") String gid){
        Map returnMap = new HashMap();
        try {
            Map map = phoneService.queryGeoAndImagePC(idCard,taskid,caseid, gid);
            returnMap.put("data",map);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //查询表单,id为子任务的id,uploadFile为图片
    @GetMapping("/queryTableFile")
    public Map queryTableFile(@RequestParam("idCard") String idCard,@RequestParam("taskid") String taskid,@RequestParam("gid") String gid){
        Map returnMap = new HashMap();
        try {
            List list = phoneService.queryTableFile(idCard,taskid,gid);
            returnMap.put("data",list);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //删除图片,mongodb中的图片
    @GetMapping("/deleteImages")
    public Map deleteImages(@RequestParam("idCard") String idCard,@RequestParam("taskid") String taskid,@RequestParam("gid") String gid){
        Map returnMap = new HashMap();
        try {
            phoneService.deleteImages(idCard,taskid,gid);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //删除table
    @GetMapping("/deleteTable")
    public Map deleteTable(@RequestParam("uuid") String uuid){
        Map returnMap = new HashMap();
        try {
            phoneService.deleteTable(uuid);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }

    //删除图片,mongodb中的图片
    @GetMapping("/deleteImage")
    public Map deleteImage(@RequestParam("filename") String filename){
        Map returnMap = new HashMap();
        try {
            phoneService.deleteImage(filename);
            returnMap.put("success",true);
            returnMap.put("resMsg","获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnMap.put("success",false);
            returnMap.put("resMsg","异常");
        }
        return returnMap;
    }
}

